#!/usr/bin/env groovy
pipeline {
    agent any
    environment {
        git_auth = "ff38538b-8879-4b12-8066-21e79b7cafe0"
    }
    stages {
        stage('拉取git仓库代码') {
            steps {
                sh 'echo $JOB_NAME'
               checkout([$class: 'GitSCM', branches: [[name: '*/master']],
               doGenerateSubmoduleConfigurations: false, extensions: [],
               submoduleCfg: [], userRemoteConfigs: [[credentialsId: "${git_auth}",
               url: 'http://192.168.1.112:9999/root/demo.git']]])
               sh 'echo ${extra_params}'
            }
        }
        stage('编译代码') {
            steps {
             sh """
                JAVA_HOME=/usr/local/jdk
                PATH=$JAVA_HOME/bin:/usr/local/maven/bin:$PATH
                mvn clean package -Dmaven.test.skip=true
                """
            }
        }
        stage('deploy project') {
            steps {
                sshPublisher(publishers: [sshPublisherDesc(configName: '123.207.97.65-ssh',
                transfers: [sshTransfer(cleanRemote: true, excludes: '',
                execCommand: '''
                export PATH=/usr/local/jdk1.8.0_261/bin:$PATH
                if [ `ps -ef | grep java| grep -v grep|awk '{print $2}'`>0 ]; then kill `ps -ef | grep java| grep -v grep|awk '{print $2}'`; fi
                java -version
                nohup java -jar /data/run/demo.jar --spring.profiles.active=product >/data/`date +%Y-%m-%d`-log.txt 2>&1 &
                ''', execTimeout: 120000,
                flatten: false, makeEmptyDirs: false,
                noDefaultExcludes: false, patternSeparator: '[, ]+',
                remoteDirectory: '/', remoteDirectorySDF: false,
                removePrefix: 'target', sourceFiles: 'target/demo.jar')],
                usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
            }
        }
    }
}
